package com.factory.test.util;

import android.content.res.Resources;
import android.util.Log;
import android.view.KeyEvent;

import com.factory.test.base.MyApplication;

import java.io.DataOutputStream;

public class ShellUtils {

    private static boolean isRoot = true;

    public static void black(){
        String[] commands = new String[]{"input keyevent " + KeyEvent.KEYCODE_BACK};
        runCommand(commands);
    }

    public static int getNavigationBarHeight() {
        Resources resources = MyApplication.getMyApplication().getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    public static void hideNavigationBar() {
        String[] commands = new String[]{"wm overscan 0,0,0,-" + getNavigationBarHeight()};
        runCommand(commands);
    }

    public static void showNavigationBar() {
        String[] commands = new String[]{"wm overscan 0,0,0,0"};
        runCommand(commands);
    }

    public static void stopTcpAdbDebug(){
        String[] commands = new String[]{"setprop service.adb.tcp.port -1", "stop adbd", "start adbd"};
        runCommand(commands);
    }

    public static void startTcpAdbDebug(){
        String[] commands = new String[]{"setprop service.adb.tcp.port 5555", "stop adbd", "start adbd"};
        runCommand(commands);
    }

    public static void setInstallNonMarketApps(boolean allow){
       /*
        adb shell settings put secure install_non_market_apps 1
        adb shell settings get secure install_non_market_apps
        可以获取状态
        0为关闭，1为开启
        */
        String[] commands = new String[]{"settings put secure install_non_market_apps " + (allow ? "1" : "0")};
        runCommand(commands);
    }

    private static void runCommand(String[] commands) {
        Process process = null;
        DataOutputStream dataOutputStream = null;

        try {
            if(isRoot){
                process = Runtime.getRuntime().exec("su");
            }else{
                process = Runtime.getRuntime().exec("sh");
            }
            dataOutputStream = new DataOutputStream(process.getOutputStream());
            int length = commands.length;

            for (int i = 0; i < length; ++i) {
                String command = commands[i];
                dataOutputStream.writeBytes(command + "\n");
            }

            dataOutputStream.writeBytes("exit\n");
            dataOutputStream.flush();
            process.waitFor();
        } catch (Exception e) {
            Log.e("adb", "shell exec fail ", e);
        } finally {
            try {
                if (dataOutputStream != null) {
                    dataOutputStream.close();
                }

                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
